/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cult.ticketing.dao;

import cult.ticketing.bean.Movie;
import cult.ticketing.jdbc.DB;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author 51158282
 */
public class MovieDAO {
    /**
     * Display a list of movies
     * @param status - a Boolean specifying the status of a movie (true: now showing; false: upcoming)
     * @return a list of Movie objects now showing or upcoming
     */
    public ArrayList<Movie> getAllMovies(Boolean status) {
        ArrayList<Movie> movies = new ArrayList<Movie>();

        //[id], [title], [available_date], [director], [actor], [intro], [status], [image_url]
        
        try {
            DataSource ds = DB.getDataSource();
            Connection con = ds.getConnection();
            PreparedStatement pstmt = con.prepareStatement("SELECT * FROM [movie] where [status] = ?;");
            pstmt.setBoolean(1, status);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs != null && rs.next() != false) {
                Movie movie = new Movie();
                movie.setId(rs.getInt(1));
                movie.setTitle(rs.getString(2));
                movie.setAvailableDate(rs.getString(3));
                movie.setDirector(rs.getString(4));
                movie.setActor(rs.getString(5));
                movie.setIntro(rs.getString(6));
                movie.setStatus(rs.getBoolean(7));
                movie.setImageURL(rs.getString(8));

                movies.add(movie);
            }

            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return movies;
    }

    public ArrayList<Movie> getAllMovies() {
        ArrayList<Movie> movies = new ArrayList<Movie>();

        //[id], [title], [available_date], [director], [actor], [intro], [status], [image_url]

        try {
            DataSource ds = DB.getDataSource();
            Connection con = ds.getConnection();
            PreparedStatement pstmt = con.prepareStatement("SELECT * FROM [movie];");
            ResultSet rs = pstmt.executeQuery();

            while (rs != null && rs.next() != false) {
                Movie movie = new Movie();
                movie.setId(rs.getInt(1));
                movie.setTitle(rs.getString(2));
                movie.setAvailableDate(rs.getString(3));
                movie.setDirector(rs.getString(4));
                movie.setActor(rs.getString(5));
                movie.setIntro(rs.getString(6));
                movie.setStatus(rs.getBoolean(7));
                movie.setImageURL(rs.getString(8));

                movies.add(movie);
            }

            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return movies;
    }

    /**
     * Search a movie by title
     * @param title - a String specifying the title of a movie
     * @return a Movie object that matches the given title
     */
    public  ArrayList<Movie> getMovieByTitle(String title) {
        ArrayList<Movie> movies = new ArrayList<Movie>();

        //[id], [title], [available_date], [director], [actor], [intro], [status]

        try {
            DataSource ds = DB.getDataSource();
            Connection con = ds.getConnection();
            PreparedStatement pstmt = con.prepareStatement("SELECT * FROM [movie] where [title] = ?;");
            pstmt.setString(1, title);
            ResultSet rs = pstmt.executeQuery();

            while (rs != null && rs.next() != false) {
                Movie movie = new Movie();
                movie.setId(rs.getInt(1));
                movie.setTitle(rs.getString(2));
                movie.setAvailableDate(rs.getString(3));
                movie.setDirector(rs.getString(4));
                movie.setActor(rs.getString(5));
                movie.setIntro(rs.getString(6));
                movie.setStatus(rs.getBoolean(7));
                movie.setImageURL(rs.getString(8));

                movies.add(movie);
            }

            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return movies;
    }

    public Movie getMovieById(int movieId) {
        Movie movie = null;

        //[id], [title], [available_date], [director], [actor], [intro], [status]

        try {
            DataSource ds = DB.getDataSource();
            Connection con = ds.getConnection();
            PreparedStatement pstmt = con.prepareStatement("SELECT * FROM [movie] where [id] = ?;");
            pstmt.setInt(1, movieId);
            ResultSet rs = pstmt.executeQuery();
            while (rs != null && rs.next() != false) {
                movie = new Movie();
                movie.setId(rs.getInt(1));
                movie.setTitle(rs.getString(2));
                movie.setAvailableDate(rs.getString(3));
                movie.setDirector(rs.getString(4));
                movie.setActor(rs.getString(5));
                movie.setIntro(rs.getString(6));
                movie.setStatus(rs.getBoolean(7));
            }

            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return movie;
    }

    public Movie updateMovieById(String name,String date,String director,String actor,String introuction,boolean status,String url,int movieId) {
        Movie movie = null;

        try {
            DataSource ds = DB.getDataSource();
            Connection con = ds.getConnection();
            PreparedStatement pstmt = con.prepareStatement("UPDATE [movie] SET [title] = ?, [available_date] = ?,[director] = ?, [actor] = ?, [intro] = ?, [status] = ?, [image_url] = ? WHERE [id] = ?;");
            pstmt.setString(1, name);
            pstmt.setString(2, date);
            pstmt.setString(3, director);
            pstmt.setString(4, actor);
            pstmt.setString(5, introuction);
            pstmt.setBoolean(6, status);
            pstmt.setString(7, url);
            pstmt.setInt(8, movieId);

            ResultSet rs = pstmt.executeQuery();
            while (rs != null && rs.next() != false) {
                movie = new Movie();
                movie.setId(rs.getInt(1));
                movie.setTitle(rs.getString(2));
                movie.setAvailableDate(rs.getString(3));
                movie.setDirector(rs.getString(4));
                movie.setActor(rs.getString(5));
                movie.setIntro(rs.getString(6));
                movie.setStatus(rs.getBoolean(7));
            }

            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return movie;
    }

    /*
     	public static int getTotalMovieCount() {
		int count = 0;
		// TODO retrieve data from db
		//count = select count id from movie


		return count;
	}

	public static Movie[] getMovieByStatus(boolean status) {
		// TODO retrieve from database
		// rs = select * from movie where movie_status = status
		int movieCount = 0; // movieCount = size of rs
		Movie[] movies = new Movie[movieCount];

		for (int i=0; i<movieCount; i++) {
			// TODO use constructor to initiate movies[i]
			// movies[i] = new Movie(rs.getInt("id"), etc)
		}

		return movies;

	}

	public static Movie[] getMovieByName(String movieName) {
		// TODO retrieve from database
		// rs = select * from movie where name include movieName
		int movieCount = 0; // movieCount = size of rs
		Movie[] movies = new Movie[movieCount];

		for (int i=0; i<movieCount; i++) {
			// TODO use constructor to initiate movies[i]
			// movies[i] = new Movie(rs.getInt("id"), etc)
		}

		return movies;
	}

	public static Movie[] getMovieByDate(Calendar date) {
		// TODO retrieve from database
		// rs = select * from movie where today > available_date
		int movieCount = 0; // movieCount = size of rs
		Movie[] movies = new Movie[movieCount];

		for (int i=0; i<movieCount; i++) {
			// TODO use constructor to initiate movies[i]
			// movies[i] = new Movie(rs.getInt("id"), etc)
		}

		return movies;
	}

	public static Movie getMovieById(int movieId) {
		// TODO rs = select * from movie where id = movieId
		Movie movie = null;

		// TODO  use constructor to initiate movie
		// movie = new Movie(rs.getInt("id"), etc)

		return movie;

	}

	public static boolean setMovieById(int movieId, String name, String director, String[] actor, String intro, String availableDate) {
		boolean ifSetSuccessful = false;
		// TODO rs = update .... from movie where id = movieId

		return ifSetSuccessful;
	}

	public static boolean deleteMovieById(int movieId) {
		boolean ifDeleteSuccessful = false;
		// TODO rs = delete .... from movie where id = movieId

		return ifDeleteSuccessful;
	}
 */
	public boolean addNewMovie(String name, String date, String director, String actor, String introduction, boolean status, String url) {
		boolean ifAddSuccessful = true;
		 try {
                    DataSource ds = DB.getDataSource();
                    Connection conn = ds.getConnection();
                    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO movie([title], [available_date], [director], [actor], [intro], [status], [image_url])VALUES(?, ?, ?, ?, ?, ? ,?);");
                    pstmt.setString(1, name);
                    pstmt.setString(2, date);
                    pstmt.setString(3, director);
                    pstmt.setString(4, actor);
                    pstmt.setString(5, introduction);
                    pstmt.setBoolean(6, status);
                    pstmt.setString(7, url);

                        if (pstmt.executeUpdate() == 0) {
                            ifAddSuccessful = false;
                        }


                        if (pstmt != null) {
                            pstmt.close();
                        }
                        if (conn != null) {
                            conn.close();
                        }
                    } catch (NamingException ne) {

                    } catch (SQLException sqle) {

                    }

		return ifAddSuccessful;
	}



    

}
